package com.tuzhi.util;


import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RangeUtils {
    //匹配范围表达式：(1,8.6)、[1,8.6)、[1,8.6]、(1,8.6]
    private final static String RANGE_REGEX_STR = "\\s*(?<minComparator>[\\(\\[])\\s*(?<minRangeNum>-?(\\d)+(\\.\\d+)*)\\s*,\\s*(?<maxRangeNum>-?(\\d)+(\\.\\d+)*)\\s*(?<maxComparator>[\\)\\]])\\s*";
    private final static String gt = "(";
    private final static String gtEq = "[";
    private final static String ltEq = "]";
    private final static String lt = ")";

    /**
     * Description:是否符合最小数值条件
     *
     * @param minNum:最小数值
     * @param num:被比较的数值
     * @param comparator:比较符
     * @return boolean
     */
    public static boolean minCompare(BigDecimal minNum, BigDecimal num, String comparator) {
        if (gt.equals(comparator)) {
            if (num.compareTo(minNum) > 0) {
                return true;
            }
        }
        if (gtEq.equals(comparator)) {
            if (num.compareTo(minNum) >= 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * Description: 是否符合最大数值条件
     *
     * @param maxNum:最大数值
     * @param num:被比较的数值
     * @param comparator:比较符
     * @return boolean
     */
    public static boolean maxCompare(BigDecimal maxNum, BigDecimal num, String comparator) {
        if (lt.equals(comparator)) {
            if (num.compareTo(maxNum) < 0) {
                return true;
            }
        }
        if (ltEq.equals(comparator)) {
            if (num.compareTo(maxNum) <= 0) {
                return true;
            }
        }
        return false;
    }


    public static boolean inRange(BigDecimal val, String rangeExpression) {
        if (!rangeExpression(rangeExpression)) {
            throw new IllegalArgumentException("非法范围表达式：" + rangeExpression);
        }
        Pattern p = Pattern.compile(RANGE_REGEX_STR);
        Matcher matcher = p.matcher(rangeExpression);
        BigDecimal leftNum = null;
        String minComparator = null;
        String maxComparator = null;
        BigDecimal rightNum = null;
        if (matcher.find()) {
            String minRangeNum = matcher.group("minRangeNum");
            leftNum = new BigDecimal(minRangeNum);
            minComparator = matcher.group("minComparator");
            maxComparator = matcher.group("maxComparator");
            String maxRangeNum = matcher.group("maxRangeNum");
            rightNum = new BigDecimal(maxRangeNum);
        }
       /* if (leftNum.compareTo(rightNum) > 0) {
            throw new IllegalArgumentException("范围表达式左区域（>或>=）的数值不能比右区域（<或<=）的数值大：" + rangeExpression);
        }*/
        if (minCompare(leftNum, val, minComparator) && maxCompare(rightNum, val, maxComparator)) {
            return true;
        }
        return false;
    }

    /**
     * Description: 判断是否合格的范围表达式
     *
     * @param rangeExpression:
     * @return boolean
     */
    public static boolean rangeExpression(String rangeExpression) {
        Pattern p = Pattern.compile(RANGE_REGEX_STR);
        Matcher matcher = p.matcher(rangeExpression);
        return matcher.matches();
    }

}
